package com.wodsy.phoenix.dataservice.shopc.service.impl;

import java.util.Date;
import java.util.List;

import com.wodsy.phoenix.datadao.shopc.dao.*;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.base.PhoenixGenericResponse;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.base.ResponseCodeConstants;
import com.wodsy.phoenix.dataobject.shopc.model.*;
import com.wodsy.phoenix.dataservice.shopc.service.ShopService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;

@Service(interfaceName="com.wodsy.phoenix.dataservice.shopc.service.ShopService" )
public class ShopServiceImpl implements ShopService {

	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private ShopAuditDao shopAuditDao;
	@Autowired
	private ShopDao shopDao;
	@Autowired
	private ShopPolicyDao shopPolicyDao;
	@Autowired
	private ShopFavoriteDao shopFavoriteDao;
	@Autowired
	private ShopReportDao shopReportDao;
	@Autowired
	private ShopkeeperAccountDao shopkeeperAccountDao;
	@Autowired
	private ShopkeeperAuthInfoDao shopkeeperAuthInfoDao;
	@Autowired
	private ShopkeeperRecommendGoodsDao shopkeeperRecommendGoodsDao;
	@Autowired
	private PromiseDao promiseDao;

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> passShopAudit(Integer shopId, Integer shopOwnerId,
			Integer operId) {
		ShopAudit audit = new ShopAudit(shopId, shopOwnerId,true,"", operId, new Date());
		shopAuditDao.saveOrUpdate(audit);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> denyShopAudit(Integer shopId, Integer shopOwnerId,
		Integer operId, String denyReason) {
		ShopAudit audit = new ShopAudit(shopId, shopOwnerId,false,denyReason, operId, new Date());
		shopAuditDao.saveOrUpdate(audit);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopAudit> getShopAudit(Integer shopId) {
		try {
			ShopAudit audit = shopAuditDao.get(shopId);
			if(audit == null){
                audit = new ShopAudit();
                audit.setShopId(shopId);
                audit.setAuditStatus(false);
                audit.setAuditFailReason("未找到审核记录");
            }
			return new PhoenixResponseWrapper<ShopAudit>(audit);
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopAudit>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addShop(Shop shop) {
			shop.setOpenTime(new Date());
	//		shop.setShutdowned(false);
	//		shop.setDeleted(false);
			shop.setCreateTime(new Date());
			shopDao.save(shop);
			return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(shop.getId()));
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delShop(Integer shopId) {
		shopDao.delShop(shopId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<Shop> getShop(Integer shopId) {
		try {
			Shop shop = shopDao.get(shopId);
			if (shop != null){
				return new PhoenixResponseWrapper<Shop>(shop);
			}else{
				return new PhoenixResponseWrapper<Shop>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<Shop>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<Shop> findShop(Pager<Shop> pager) {
		try {
			Pager<Shop> pager1 = shopDao.findShop(pager);
			if (pager1.getItems() != null && !pager1.getItems().isEmpty()){
				return new PhoenixResponseWrapper<Shop>(pager1);
			}else {
				return new PhoenixResponseWrapper<Shop>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<Shop>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<Shop> updateShop(Shop shop) {
		shopDao.update(shop);
		return new PhoenixResponseWrapper<Shop>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<Shop> openShop(Integer shopId) {
		shopDao.openShop(shopId);
		return new PhoenixResponseWrapper<Shop>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<Shop> closeShop(Integer shopId) {
		shopDao.closeShop(shopId);
		return new PhoenixResponseWrapper<Shop>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addShopPolicy(ShopPolicy policy) {
		policy.setCreateTime(new Date());
		shopPolicyDao.save(policy);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(policy.getShopId()));
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<ShopPolicy> updateShopPolicy(ShopPolicy policy) {
		policy.setUpdateTime(new Date());
		shopPolicyDao.update(policy);
		return new PhoenixResponseWrapper<ShopPolicy>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopPolicy> getShopPolicy(Integer shopId) {
		try {
			ShopPolicy shopPolicy = shopPolicyDao.get(shopId);
			if (shopPolicy != null){
				return new PhoenixResponseWrapper<ShopPolicy>(shopPolicy);
			}else{
				return new PhoenixResponseWrapper<ShopPolicy>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopPolicy>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<Promise> findPromises() {
		try {
			List<Promise> promises = promiseDao.findByAll();
			if (promises != null && !promises.isEmpty()){
				return new PhoenixResponseWrapper<Promise>(promises);
			}else {
				return new PhoenixResponseWrapper<Promise>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<Promise>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<Promise> getPromise(int id) {
		try {
			Promise promise = promiseDao.get(id);
			if (promise != null){
                return new PhoenixResponseWrapper<Promise>(promise);
            }else {
                return new PhoenixResponseWrapper<Promise>(ResponseCodeConstants.EMPTY, "empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<Promise>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addShopFavorite(Integer userId, Integer shopId) {
		shopFavoriteDao.addShopFavorite(userId, shopId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse());
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delShopFavorite(Integer userId, Integer shopId) {
		shopFavoriteDao.delShopFavorite(userId, shopId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse());
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopFavorite> findShopFavoritesByUserId(
			Pager<ShopFavorite> pager, Integer userId) {
		try {
			Pager<ShopFavorite> pager1 = shopFavoriteDao.findShopFavoritesByUserId(pager, userId);
			if (pager1.getItems() != null && !pager1.getItems().isEmpty()){
				return new PhoenixResponseWrapper<ShopFavorite>(pager1);
			}else {
				return new PhoenixResponseWrapper<ShopFavorite>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopFavorite>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopFavorite> findShopFavoritesByUserId(Integer userId, Integer shopId){
		try {
			ShopFavorite shopFavorite = shopFavoriteDao.getShopFavorite(userId, shopId);
			if (shopFavorite != null){
                return new PhoenixResponseWrapper<ShopFavorite>(shopFavorite);
            }else {
                return new PhoenixResponseWrapper<ShopFavorite>(ResponseCodeConstants.EMPTY, "empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopFavorite>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> reportShop(ShopReport report) {
		report.setReportTime(new Date());
		shopReportDao.save(report);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(report.getId()));
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopReport> findShopReportByShopId(Pager<ShopReport> pager,
			Integer shopId) {
		try {
			Pager<ShopReport> pager1 = shopReportDao.findShopReportByShopId(pager, shopId);
			if (pager1.getItems() != null && !pager1.getItems().isEmpty()){
				return new PhoenixResponseWrapper<ShopReport>(pager1);
			}else {
				return new PhoenixResponseWrapper<ShopReport>(ResponseCodeConstants.EMPTY, "empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopReport>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addShopkeeperAccount(ShopkeeperAccount account) {
		account.setCreateTime(new Date());
		shopkeeperAccountDao.save(account);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(account.getAccountId()));
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delShopkeeperAccount(Integer accountId) {
		shopkeeperAccountDao.delShopkeeperAccount(accountId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}
	
	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> disableShopkeeperAccount(Integer accountId) {
		shopkeeperAccountDao.disableShopkeeperAccount(accountId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> enableShopkeeperAccount(Integer accountId) {
		shopkeeperAccountDao.enableShopkeeperAccount(accountId);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> updateShopkeeperAccount(ShopkeeperAccount account) {
		account.setUpdateTime(new Date());
		shopkeeperAccountDao.update(account);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopkeeperAccount> findShopkeeperAccountByShopId(Integer shopId) {
		try {
			List<ShopkeeperAccount> list = shopkeeperAccountDao.findAllByProperty("shopId", shopId);
			if (list != null && !list.isEmpty()){
                Pager<ShopkeeperAccount> pager = new Pager<ShopkeeperAccount>();
                pager.setItems(list);
                pager.setTotalCount(list.size());
                return new PhoenixResponseWrapper<ShopkeeperAccount>(pager);
            }else {
                return new PhoenixResponseWrapper<ShopkeeperAccount>(ResponseCodeConstants.EMPTY,"empty result");
            }
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopkeeperAccount>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopkeeperAccount> findShopkeeperAccountByUserId(Integer userId) {
		try {
			List<ShopkeeperAccount> list = shopkeeperAccountDao.findAllByProperty("userId", userId);
			if (list != null && !list.isEmpty()){
				Pager<ShopkeeperAccount> pager = new Pager<ShopkeeperAccount>();
				pager.setItems(list);
				pager.setTotalCount(list.size());
				return new PhoenixResponseWrapper<ShopkeeperAccount>(pager);
			}else {
				return new PhoenixResponseWrapper<ShopkeeperAccount>(ResponseCodeConstants.EMPTY,"empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopkeeperAccount>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addShopkeeperAuthInfo(ShopkeeperAuthInfo auth) {
		auth.setCreateTime(new Date());
		shopkeeperAuthInfoDao.save(auth);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(auth.getShopId()));
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> updateShopkeeperAuthInfo(ShopkeeperAuthInfo auth) {
		auth.setUpdateTime(new Date());
		shopkeeperAuthInfoDao.update(auth);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopkeeperAuthInfo> getShopkeeperAuthInfo(Integer shopId) {
		try {
			ShopkeeperAuthInfo auth = shopkeeperAuthInfoDao.get(shopId);
			if (auth != null){
				return new PhoenixResponseWrapper<ShopkeeperAuthInfo>(auth);
			}else {
				return new PhoenixResponseWrapper<ShopkeeperAuthInfo>(ResponseCodeConstants.EMPTY,"empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopkeeperAuthInfo>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> addGoodRecommend(Integer shopId, Integer goodId, Integer weight) {
		ShopkeeperRecommendGoods rec = new ShopkeeperRecommendGoods(shopId, goodId, weight, new Date());
		shopkeeperRecommendGoodsDao.save(rec);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>(new PhoenixGenericResponse(rec.getId()));
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> delGoodRecommend(Integer id) {
		ShopkeeperRecommendGoods rec = new ShopkeeperRecommendGoods();
		rec.setId(id);
		shopkeeperRecommendGoodsDao.delete(rec);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",rollbackFor=Exception.class)
	public PhoenixResponseWrapper<PhoenixGenericResponse> updateGoodRecommendWeight(Integer id, Integer weight) {
		ShopkeeperRecommendGoods rec = shopkeeperRecommendGoodsDao.get(id);
		rec.setWeight(weight);
		rec.setOperTime(new Date());
		shopkeeperRecommendGoodsDao.update(rec);
		return new PhoenixResponseWrapper<PhoenixGenericResponse>();
	}

	@Override
	@Transactional(value="xa",readOnly=true)
	public PhoenixResponseWrapper<ShopkeeperRecommendGoods> findGoodRecommends(Integer shopId) {
		try {
			List<ShopkeeperRecommendGoods> list = shopkeeperRecommendGoodsDao.findGoodRecommends(shopId);
			if (list != null && !list.isEmpty()){
				Pager<ShopkeeperRecommendGoods> pager = new Pager<ShopkeeperRecommendGoods>();
				pager.setItems(list);
				pager.setTotalCount(list.size());
				return new PhoenixResponseWrapper<ShopkeeperRecommendGoods>(pager);
			}else {
				return new PhoenixResponseWrapper<ShopkeeperRecommendGoods>(ResponseCodeConstants.EMPTY,"empty result");
			}
		} catch (Exception e) {
			logger.error("", e);
			return new PhoenixResponseWrapper<ShopkeeperRecommendGoods>(ResponseCodeConstants.EXCEPTION,"exception occur");
		}
	}
	
	
	public static void main(String[] args) {
		com.alibaba.dubbo.container.Main.main(args);
	}

}
